/*
 * @Copyright: Copyright (c) 2030 吴周海
 * @Author: wzh200x@126.com
 * @Date: 2025-10-10 11:35:00
 * @LastEditors: wzh200x@126.com
 * @LastEditTime: 2025-10-10 14:08:29
 * @FilePath: \goWebServer\goGameWebServer\internal\model\entity\kuaiShouReportModel.go
 * @Description: KuaiShouReport 实体模型
 */
package entity

import (
	"goGameWebServer/internal/common/logger"
	"goGameWebServer/internal/common/utils"
	"goGameWebServer/internal/defines/cerror"
	"goGameWebServer/internal/defines/dataType"
	"goGameWebServer/internal/interfaces"
	"goGameWebServer/internal/manager/databasemanager"

	"github.com/gogf/gf/v2/errors/gcode"
	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/util/gmeta"
)

type KuaiShouReportModelData struct {
	gmeta.Meta          `orm:"table: kuai_shou_report"`
	Id                  dataType.TypeDataBaseId `orm:"id"`                  // 主键
	ThirdAccount        string                  `orm:"thirdAccount"`        // 第三方账号
	ActionCode          string                  `orm:"actionCode"`          // 行动代码
	EventProps          string                  `orm:"eventProps"`          // 属性信息
	ActionFinishChannel string                  `orm:"actionFinishChannel"` // 渠道(广告ID)
	OutBizNo            string                  `orm:"outBizNo"`            // 全局唯一标识
	ActionFinishDate    string                  `orm:"actionFinishDate"`    // 创建时间
	Status              int                     `orm:"status"`              // 上报状态
	Response            string                  `orm:"response"`            // 上报返回
}

func (d *KuaiShouReportModelData) Reset() {
	d.Id = 0
	d.ThirdAccount = ""
	d.ActionCode = ""
	d.EventProps = ""
	d.ActionFinishChannel = ""
	d.OutBizNo = ""
	d.ActionFinishDate = ""
	d.Status = 0
	d.Response = ""
}

type KuaiShouReportModel struct {
	EntityModel
}

func (m *KuaiShouReportModel) LoadEntityDataFromDB(records interface{}) error {
	base, err := databasemanager.ManagerDatabase().DefaultDataBase()
	if err != nil {
		return err
	}
	if entityData, ok := m.EntityData.(*KuaiShouReportModelData); ok {
		if entityData.ThirdAccount == "" || entityData.ActionCode == "" {
			return gerror.NewCodef(gcode.New(cerror.ERROR_ENTITY_PROPERTY_INVALID, "", nil), "kuai_shou_report model load need thirdAccount and actionCode")
		}
		one, err := base.GetDb().Model(m.Template).
			Where("thirdAccount", entityData.ThirdAccount).
			Where("actionCode", entityData.ActionCode).One()
		if err != nil {
			return err
		}
		if one == nil {
			return m.Create()
		}
		_ = one.Struct(m.EntityData)
		m.SetEntityDBID(entityData.Id)
		return nil
	}
	return gerror.NewCodef(gcode.New(cerror.ERROR_ENTITY_PROPERTY_INVALID, "", nil), "kuai_shou_report model data is invalid")
}

func (m *KuaiShouReportModel) Create() error {
	if entityData, ok := m.EntityData.(*KuaiShouReportModelData); ok {
		entityData.ActionFinishDate = utils.GetNowTimeFormat()
		err := m.CreateDataToDB()
		entityData.Id = m.EntityDBID
		if err != nil {
			return err
		}
		logger.LoggerIns().Debug("create kuai_shou_report success")
		return nil
	}
	return gerror.NewCodef(gcode.New(cerror.ERROR_ENTITY_PROPERTY_INVALID, "", nil), "kuai_shou_report model create fail, data is invalid")
}

func NewKuaiShouReportModel(name string) interfaces.IEntityModel {
	m := &KuaiShouReportModel{
		EntityModel: NewEntityModel(name),
	}
	m.SetEntityData(&KuaiShouReportModelData{
		Id:                  0,
		ThirdAccount:        "",
		ActionCode:          "",
		EventProps:          "",
		ActionFinishChannel: "",
		OutBizNo:            "",
		ActionFinishDate:    "",
		Status:              0,
		Response:            "",
	})
	m.SetTemplate(m.EntityData)
	return m
}
